// pages/index/newProduct/list.js
//获取工具类的应用实例  
const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

const App = getApp();

Page({
  data: {
    count: 0,

    //页面适配
    imgwidth: 0,
    imgheight: 0,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    windowHeight: wx.getSystemInfoSync().windowHeight,
    //wxml渲染层参数
    goods_data: {
      list: [],
      page: {
        page_no: 0,
        page_size: 10,
      },
      paginate: {}
    }
  },

  //初始化数据加载
  onLoad() {
    this.onPullDownRefresh()
    //this.getNewPeopleList();

  },




  //加载新品预告列表分页
  getNewPeopleList() {
    const self = this;

    this.data.count++;

    console.log("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK     " + this.data.count)


    let pageData = {
      page_no: this.data.goods_data.page.page_no + 1,
      page_size: this.data.goods_data.page.page_size,
    }

    let data = {
      page: JSON.stringify(pageData),
      service: 'psGoods.smallprogram.freeGetGoodsPreSaleListPageFind',
      params: '{}',

      url_type: 'goods',

      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code
    }

    var goods_data = this.data.goods_data

    api.reqData({
      data,
      success: (res) => {



        console.log("============加载新品预告列表分页========================")
        console.log("============加载新品预告列表分页========================")
        console.log(res.data)
        console.log(res.data.data)



        //图片地址由https 改为 http
        var newProductlist = res.data.data.list
        for (var i = 0; i < newProductlist.length; i++) {
          newProductlist[i].pic_info_url = newProductlist[i].pic_info_url.replace("https", "http")
        }


        var goodList = [...goods_data.list, ...newProductlist]


        var iDays;
        for (var i = 0; i < goodList.length; i++) {
          if (goodList[i].taobao_price == '') {
            goodList[i].taobao_link = (goodList[i].market_price - goodList[i].discount_price).toFixed(2);}
          else { 
            goodList[i].taobao_link = (goodList[i].taobao_price - goodList[i].discount_price).toFixed(2);}
          
          if (goodList[i].valid_thru!=null){

            var time1 = new Date().Format(goodList[i].valid_thru)
            var goodtime = goodList[i].valid_thru.substring(0, 10)

            var now = new Date().Format("yyyy-MM-dd");

            console.log("now>>>>>>>>>>>>>>" + now)
            console.log("goodtime>>>>>>>>>>>>>>" + goodtime)

            var timestr1 = goodList[i].valid_thru.substring(5, 10)
            var timestr2 = goodList[i].valid_thru.substring(11, 16)

            iDays = this.getDays(goodtime, now)

            if (iDays == 0) {
              goodList[i].timestr1 = "今天"
            } else if (iDays == 1) {
              goodList[i].timestr1 = "明天"
            } else if (iDays == 2) {
              goodList[i].timestr1 = "后天"
            } else {

              goodList[i].timestr1 = timestr1
            }
            goodList[i].timestr2 = timestr2


          }

          
        }

        goods_data.list = goodList


        goods_data.page.page_no = res.data.data.page.page_no
        goods_data.page.page_size = res.data.data.page.page_size
        //paginate.total_page = res.data.data.page.total_page
        goods_data.paginate.total_count = res.data.data.page.total_count
        //判断hasNext
        if (goods_data.paginate.total_count > goods_data.page.page_no * goods_data.page.page_size) {
          console.log("后面有数据 还没有查询完")
          goods_data.paginate.hasNext = true
        } else {
          console.log("后面没有数据了，不要再拉了")
          goods_data.paginate.hasNext = false
        }
        this.setData({
          goods_data: goods_data,
        })
      }
    })
  },




  getDays(strDateStart, strDateEnd){
    var strSeparator = "-"; //日期分隔符
    var oDate1;
    var oDate2;
    var iDays;
    oDate1= strDateStart.split(strSeparator);
    oDate2= strDateEnd.split(strSeparator);



    //console.log(oDate1)
    //console.log(oDate2)

    var strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2]);
    var strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2]);
    iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)//把相差的毫秒数转换为天数 
   return iDays ;
  },




  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    //if (!this.data.goods_data.paginate.hasNext) return


    console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

    wx.showLoading({
      title: '加载中',
    })


    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)

    //this.initData()
    this.getNewPeopleList()



  },

  toDetail: function (e) {

    let source = e.currentTarget.dataset.source;
    let goods_id = e.currentTarget.id;

    if (source == 'meitianhui') {
      wx.navigateTo({
        url: `../../buy/detail/selfEmployed/selfEmployed?goods_id=` + goods_id,
      });
    } else {
      wx.navigateTo({
        url: `../../buy/detail/taotaoling/taotaoling?goods_id=` + goods_id,
      });
    }

  },


  //商品图片适配
  imageLoad: function (e) {
    var windowWidth = this.data.windowWidth;
    //新人专享列表查询图片宽高：589 855
    var $width = e.detail.width;

    //设置图片显示宽度,适配0.92的比例，
    var viewWidth = windowWidth;
    var viewHeight = viewWidth;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },

  /**
   * 页面上拉事件的处理函数
   */
  onReachBottom() {
    console.info('onReachBottom')

    //if (!this.data.goods_data.paginate.hasNext) return


    if (!this.data.goods_data.paginate.hasNext) {
      console.log("到底咯！！！！！！！！！！！！！！！！！！")
      return
    }



    this.getGoods()

  },

})